package GUI;
// test

import java.awt.Color;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import Base.Movie;
import Base.MovieCompilation;
import InOut.DirReadInOut;
import InOut.LanguageSelectionInOut;

/**
 * This class creates 3 jLists. figures, backgrounds and hidden elements and shows them.
 * Icons will be added to the list model, which will be added to the lists. The model
 * can change dynamically.
 * @author Wayang Group Bremen
 * @version 0.9
 */
public class CharacterSelectionGui extends JTabbedPane implements ChangeListener,ListSelectionListener {

	private static final long serialVersionUID = 1L;
	public MovieCompilation story;
	public JScrollPane charscrolli;
	public JScrollPane charscrolli3;
	public JList jlist1;
	public JList jlist2;
	public JList jlist3;
	//list model
	public DefaultListModel model1;
	public DefaultListModel modelHidden;
	File back = new File("Data//backgroundsicons//");
	public ActiveFigurePanelGui activeFigurePanel;
	JButton AddElement;
	public ButtonStateGui buttonState;
	public LanguageSelectionInOut langReader;

	/**
	 * Constructor. Creates a new object of this class.
	 * @param story an instance of MovieCompilation
	 * @param activeFigurePanel preview window of the selected SceneElement
	 * @param buttonState the button containing the modifier buttons
	 */
	public CharacterSelectionGui(MovieCompilation story,ActiveFigurePanelGui activeFigurePanel,ButtonStateGui buttonState,LanguageSelectionInOut langReader) {

		this.langReader = langReader;
		UIManager.put("TabbedPane.tabsOpaque", Boolean.TRUE);
		UIManager.put("TabbedPane.contentOpaque", Boolean.FALSE);
		this.updateUI();
		this.story = story;
		//this.AddElement = AddElement;
		this.buttonState = buttonState;
		this.activeFigurePanel = activeFigurePanel;
		model1 = new DefaultListModel();
		jlist1 = new JList(model1);
		//jlist1 = new ListGui(model1);
		jlist1.setBorder(null);
		//add jlist1 to the selectionListener
		jlist1.addListSelectionListener(this);
		jlist1.setLayoutOrientation(JList.VERTICAL_WRAP);
		jlist1.setVisibleRowCount(2);
		jlist1.setOpaque(false);
		jlist1.setBackground(new Color(0,0,0,0));
		add(((langReader.languageSel("Figures"))),jlist1);
		//create jlist2 with backgroundicons
		jlist2 = new JList(iconreader(back));
		jlist2.setBorder(null);
		//add jlist2 to the selectionListener
		jlist2.addListSelectionListener(this);
		jlist2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jlist2.setLayoutOrientation(JList.VERTICAL_WRAP);
		jlist2.setVisibleRowCount(1);
		jlist2.setOpaque(false);
		jlist2.setBackground(new Color(0,0,0,0));
		add(((langReader.languageSel("Backgrounds"))),   jlist2);
		modelHidden = new DefaultListModel();
		jlist3 = new JList(modelHidden);
		jlist3.setBorder(null);
		//add jlist1 to the selectionListener
		jlist3.addListSelectionListener(this);
		jlist3.setLayoutOrientation(JList.VERTICAL_WRAP);
		jlist3.setVisibleRowCount(2);
		jlist3.setVisible(false);
		jlist3.setOpaque(false);
		jlist3.setBackground(new Color(0,0,0,0));
		//charscrolli3 = new JScrollPane(jlist3);
		//charscrolli3.setBorder(null);
		add(((langReader.languageSel("Hidden"))),   jlist3);
		super.setEnabledAt(2,false);

		showList();
		//the tabbedPane will be added to the changeListener
		super.addChangeListener(this);
	}

	/**
	 * This method is reading paths to the backgroundicons and gives them back.
	 * @param dir the path to the icons
   * @return icons[] the icons as an array

	 */
	public ImageIcon[]iconreader (File dir ) {
		DirReadInOut icon_dir = new DirReadInOut();
		File[] figures = icon_dir.listDir(dir);
		ImageIcon icons[] = new ImageIcon[figures.length];

		int i=0;
		while (i<figures.length) {
			if (figures[i].getName() != "Thumbs.db") {
				icons[i]= ((new ImageIcon(figures[i].toString())));
				i++;
			}
		}
		return icons;
	}
	/**
	 * This method shows the list. It fills the model with icons. If the model is not empty, it will be filled 
	 * with available icons from the active movie. JList 3 is for the hidden SceneElements, if there are any.
	 */
	public void showList() {

		if(!story.compilation.isEmpty()) {
			super.setEnabledAt(0,true);
			super.setEnabledAt(1,true);
			Movie activeMovie = story.compilation.get(story.getActiveMovie());
			//ImageIcon icons[] = new ImageIcon[activeMovie.activeIcons.size()];

			if(!model1.isEmpty()) {
				model1.removeAllElements();
				jlist3.repaint();
				jlist3.updateUI(); 
				for(int i=0; i<activeMovie.activeIcons.size();i++) {
					//icons[i] = activeMovie.activeIcons.get(i);
					ImageIcon icon = activeMovie.activeIcons.get(i);
					model1.addElement(icon);
				}
				//super.setEnabledAt(2,true);
			}
			else {
				model1.removeAllElements();
				for(int i=0; i<activeMovie.activeIcons.size();i++) {
					//icons[i] = activeMovie.activeIcons.get(i);
					ImageIcon icon = activeMovie.activeIcons.get(i);
					model1.addElement(icon);	
				}
			}

			//if(!modelHidden.isEmpty()) {
			if(!activeMovie.HideIcons.isEmpty()) {
				modelHidden.removeAllElements();
				super.setEnabledAt(2,true);

				for(int i=0; i<activeMovie.HideIcons.size();i++) {
					// icons[i] = activeMovie.HideIcons.get(i);
					ImageIcon icon = activeMovie.HideIcons.get(i);
					modelHidden.addElement(icon);
				}
				// super.setEnabledAt(2,true);
				// jlist3.clearSelection();
				jlist3.repaint();
				jlist3.updateUI(); 
			}

			else {
				modelHidden.removeAllElements();
				jlist3.repaint();
				jlist3.updateUI();
				//jlist3.clearSelection();
				super.setEnabledAt(2,false);	
			}
		} 
	}

	/**
	 * Deletes all elements from the lists.
	 */
	public void deleteLists() {
		model1.removeAllElements();
		modelHidden.removeAllElements();
	}
	//
	/**
	 * If the selected tab changes, this method clears list selections.
	 */
	@Override
	public void stateChanged(ChangeEvent e) {
		jlist1.setEnabled(false);
		jlist1.setEnabled(true);
		jlist1.clearSelection();
		jlist2.setEnabled(false);
		jlist2.setEnabled(true);
		jlist2.clearSelection();
		jlist3.setEnabled(false);
		jlist3.setEnabled(true);
		jlist3.clearSelection();
		activeFigurePanel.clearPanel();
	}

	/**
	 * If there is a selection in one of the lists, the addElement button will be
	 * shown and the method showActiveFigure(String path) in activeFigurePanelGui will
	 * be called, which will draw a preview of the selected figure.
	 * @param e the ListSelectionEvent
	 */
	@Override
	public void valueChanged(ListSelectionEvent e) {

		if(!jlist1.isSelectionEmpty() && e.getValueIsAdjusting()==false) {
			//System.out.println("jlist1: " +jlist1.getSelectedValue().toString());
			activeFigurePanel.showActiveFigure(jlist1.getSelectedValue().toString());
			buttonState.selFigureInList();
		} 

		if(!jlist2.isSelectionEmpty() && e.getValueIsAdjusting()==false) {
			//System.out.println("jlist2: " +jlist2.getSelectedValue().toString());
			activeFigurePanel.showActiveFigure(jlist2.getSelectedValue().toString());
			buttonState.selFigureInList();
		}

		if(!jlist3.isSelectionEmpty() && e.getValueIsAdjusting()==false) {
			//System.out.println("hidden figure selected!");
			activeFigurePanel.showActiveFigure(jlist3.getSelectedValue().toString());
			buttonState.selHiddenFigureInList();
		}
	}
}
